package com.lijie.bs_recruit.controller;


import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.VO.ResultVO;
import com.lijie.bs_recruit.model.News;
import com.lijie.bs_recruit.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijie
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    NewsService newsService;

    @RequestMapping("/selNewsById")
    public ResultVO<News> selNewsById(@RequestBody News news){
        ResultVO<News> newsResultVO =new ResultVO<>();
        if(news!=null){
            News news1 = newsService.selectNewsById(news);
            if(news1!=null){
                newsResultVO.setMessage("succ");
                newsResultVO.setObject(news1);
            }else{
                newsResultVO.setMessage("error");
            }
        }
        return newsResultVO;
    }


    //admin查看所有
    @RequestMapping("/selNews/{pageIndex}")
    public PageInfo<News> selJobfaircompany(@PathVariable Integer pageIndex, @RequestBody News news){
        PageInfo<News> newsPageInfo = newsService.selectNews(pageIndex, news);
        return newsPageInfo;
    }

    @RequestMapping("/addNews")
    public ResultVO<News> addNews(@RequestBody News news){
        ResultVO<News> newsResultVO =new ResultVO<>();
        if(news!=null){
            Boolean aBoolean = newsService.addNews(news);
            if(aBoolean){
                newsResultVO.setMessage("succ");
            }else{
                newsResultVO.setMessage("error");
            }
        }
        return newsResultVO;
    }

    @RequestMapping("/updateNews")
    public ResultVO<News> updateNews(@RequestBody News news){
        ResultVO<News> newsResultVO =new ResultVO<>();
        if(news!=null){
            news.setCreateTime(null);
            news.setUpdateTime(null);
            Boolean aBoolean = newsService.updateNews(news);
            if(aBoolean){
                newsResultVO.setMessage("succ");
            }else{
                newsResultVO.setMessage("error");
            }
        }
        return newsResultVO;
    }

    //删除（下架）新闻
    @RequestMapping("/deleteNews")
    public ResultVO<News> deleteNews(@RequestBody News news){
        ResultVO<News> newsResultVO =new ResultVO<>();
        if(news!=null){
            Boolean aBoolean = newsService.updateNews(news);
            if(aBoolean){
                newsResultVO.setMessage("succ");
            }else{
                newsResultVO.setMessage("error");
            }
        }
        return newsResultVO;
    }


    //用户查看未被删除的新闻
    @RequestMapping("/selJobfaircompanyByUser/{pageIndex}")
    public PageInfo<News> selJobfaircompanyByUser(@PathVariable Integer pageIndex, @RequestBody News news){
        PageInfo<News> newsPageInfo = newsService.selectNewsByUser(pageIndex, news);
        return newsPageInfo;
    }

    //增加浏览次数
    @RequestMapping("/addNewsLookNum")
    public ResultVO<News> addNewsLookNum(@RequestBody News news){
        ResultVO<News> newsResultVO =new ResultVO<>();
        if(news!=null){
            Boolean aBoolean = newsService.addNewsLookNum(news);
            if(aBoolean){
                newsResultVO.setMessage("succ");
            }
        }
        return newsResultVO;
    }


}

